﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Model;
using BLL;
using System.Configuration;

public partial class Admin_MerchantsEdit : BasePage
{
    Access access = new Access();
    MerchantsBO mbo = new MerchantsBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //if (GetQeuryString("para") == "update")
            //{
                InitData();
            //}
            //else if (GetQeuryString("para") == "add")
            //{
            //    //this.txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //}
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            //if (GetQeuryString("para") == "update")
            //{
                Model.Merchants menu = GetEntity();
                menu.ID = 1;
                mbo.Update(menu);
                AlertMessage("保存成功");
            //}
            //else if (GetQeuryString("para") == "add")
            //{
            //    Model.Merchants m = GetEntity();
            //    //if (m.Status == 1 && gbo.GetModel() != null)
            //    //{
            //    //    AlertMessage("启用状态的团购只能有一个");
            //    //    return;
            //    //}
            //    mbo.Add(m);
            //    AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            //}
        }
        catch (Exception)
        {
            AlertMessage("请将信息填写完整");
        }
    }

    private void InitData()
    {
        string id = "1";
        Merchants ds = mbo.GetModel(int.Parse(id));
        txtToUserName.Text = ds.ToUserName;
        txtPicUrl.Text = ds.Logo;
        txtName.Text = ds.Name;
        txtIntroduce.Text = ds.Introduce;
        Image1.ImageUrl = ds.Logo;
        txtDX.Text = ds.DX;
        txtPhone.Text = ds.Phone;
        txtPic.Text = ds.Pic;
        Image2.ImageUrl = ds.Pic;
        txtAccess_token.Text = ds.Access_token;
    }
    public Model.Merchants GetEntity()
    {
        Model.Merchants m = new Model.Merchants();
        if (GetQeuryString("para") == "update")
        {
            m = mbo.GetModel(1);
        }
        else
        {

        }
        m.Introduce = txtIntroduce.Text;
        m.Logo = txtPicUrl.Text;
        m.Name = txtName.Text;
        m.ToUserName = txtToUserName.Text;
        m.DX = txtDX.Text;
        m.Phone = txtPhone.Text;
        m.Pic = txtPic.Text;
        m.Access_token = txtAccess_token.Text;
       

        return m;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string FileName = file.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".png"))
            {
                int fileSize = file.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                //else if (fileSize > 52100)
                //{
                //    AlertMessage("请上传50K以内的png格式照片");
                //}
                else
                {
                    //if (File.Exists(path + CurrentUserInfo.ID + ".png"))
                    //{
                    //    File.Delete(path + CurrentUserInfo.ID + ".png");
                    //}
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        file.SaveAs(path + guid + ".png");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".png"))
                        {
                            Image1.ImageUrl = "Images/" + guid + ".png";
                            txtPicUrl.Text = ConfigurationManager.AppSettings["ServerURL"] + "/Admin/Images/" + guid + ".png";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传png格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
        //}
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload1.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = FileUpload1.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                //else if (fileSize > 52100)
                //{
                //    AlertMessage("请上传50K以内的png格式照片");
                //}
                else
                {
                    //if (File.Exists(path + CurrentUserInfo.ID + ".png"))
                    //{
                    //    File.Delete(path + CurrentUserInfo.ID + ".png");
                    //}
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        FileUpload1.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image2.ImageUrl = "Images/" + guid + ".jpg";
                            txtPic.Text = ConfigurationManager.AppSettings["ServerURL"] + "/Admin/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }
}